package managedbean.SCM.MaterialOrder;

import entity.SCM.MaterialLine;
import entity.RawMaterial;
import entity.SCM.Supplier;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.SCM.MaterialOrderBeanLocal;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="createMaterialOrder")
@ViewScoped
public class createMaterialOrderDirectly {

    public createMaterialOrderDirectly() {
    }
    @EJB
    private MaterialOrderBeanLocal materialOrderaBeanLocal;
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    private String selectMaterial;
    private String selectSupplier;
    private Long lotQuantity;
    private String statusMessage;
    private List<String> materialTypes = new ArrayList<String>();  
    private List<String> suppliers = new ArrayList<String>();
    private List<MaterialLine> tempMaterialLines = new ArrayList<MaterialLine>();
    private int count = 0;
    
    public void initView(PhaseEvent event){
        if(count==0)
        {
            List<RawMaterial> rms = supplierBeanLocal.getAllMaterial();
            if(rms!=null)
            {
                System.out.println("test initView"+rms.size());
                for(int i=0;i<rms.size();i++)
                {
                    this.materialTypes.add(rms.get(i).getMaterialType());
                }
            }
            count++;
        }
        System.out.println("test initView final"+this.getMaterialTypes().size());
    }

    public String getSelectSupplier() {
        return selectSupplier;
    }

    public void setSelectSupplier(String selectSupplier) {
        this.selectSupplier = selectSupplier;
    }

    public List<String> getSuppliers() {
        return suppliers;   
    }

    public void setSuppliers(List<MaterialLine> mll) {
        if(this.tempMaterialLines!=null){
            List<Supplier> temp = supplierBeanLocal.getSupplierForOrder(mll);          
            if(temp==null){
                suppliers = null;
                System.out.print("No such supplier provide all the materials");
                statusMessage = "No such supplier provide all the materials";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,statusMessage, ""));
            }else{
                suppliers = new ArrayList<String>();
                for(int i=0; i<temp.size();i++){
                    suppliers.add(temp.get(i).getSupplierName());
                }  
            }
        }
    }

    public Long getLotQuantity() {
        return lotQuantity;
    }

    public void setLotQuantity(Long lotQuantity) {
        this.lotQuantity = lotQuantity;
    }

    public String getSelectMaterial() {
        return selectMaterial;
    }

    public void setSelectMaterial(String selectMaterial) {
        this.selectMaterial = selectMaterial;
    }

    public List<String> getMaterialTypes() {
        return materialTypes;
    }

    public List<MaterialLine> getTempMaterialLines() {
        return tempMaterialLines;
    }
  
    public void displayTempList(ActionEvent event) throws Exception{   
        RawMaterial rawMaterial = materialOrderaBeanLocal.getMaterial(this.selectMaterial);
        MaterialLine materialLine = new MaterialLine();
        materialLine.setRawMaterial(rawMaterial);
        materialLine.setLotQuantity(this.lotQuantity);
        tempMaterialLines.add(materialLine);
        this.setSuppliers(tempMaterialLines);
        System.out.print("+++++++---------+++++++");
        this.materialTypes.remove(this.selectMaterial);
        if(this.suppliers==null){
            System.out.print("++++++++++++++");
            statusMessage = "No such supplier provide all the materials";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,statusMessage, ""));
        }
    }
    
    public void saveMaterialOrder(ActionEvent event) throws Exception{
        try{
            if(this.selectSupplier!=null & this.tempMaterialLines!=null){
                Long id = this.materialOrderaBeanLocal.createMaterialOrder(this.selectSupplier, tempMaterialLines, null);
                if (id == null) {
                    statusMessage = "Please Try Again.";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful:" + statusMessage + "", ""));
                } else {
                    statusMessage = "New Material Purchasing Order " + id + " Saved Successfully.";
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                }
            }else if(this.selectSupplier==null){
                statusMessage = "Please choose supplier first";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, statusMessage, ""));
            }else if(this.tempMaterialLines==null){
                statusMessage = "Please add material line first";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, statusMessage, ""));
            }
        }catch(Exception ex){
            System.out.print("&&&&&&&&&&");
        }
    }  
}
